﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel.Design
Imports System.Windows.Forms.Design
Imports System.Workflow.ComponentModel

Namespace System.Workflow.ComponentModel.Design
    Friend NotInheritable Class SecondaryView
        Inherits DesignerView
        ' Methods
        Friend Sub New(ByVal parentDesigner As StructuredCompositeActivityDesigner, ByVal id As Integer, ByVal [text] As String, ByVal activityType As Type)
            MyBase.New(id, [text], ActivityToolboxItem.GetToolboxImage(activityType))
            Me.parentDesigner = parentDesigner
            MyBase.UserData.Item(SecondaryView.UserDataKey_ActivityType) = activityType
            If (Me.parentDesigner.Activity.GetType Is activityType) Then
                MyBase.UserData.Item(SecondaryView.UserDataKey_Designer) = Me.parentDesigner
            End If
        End Sub

        Public Overrides Sub OnActivate()
            If (Me.AssociatedDesigner Is Nothing) Then
                Dim type As Type = [TryCast](MyBase.UserData.Item(SecondaryView.UserDataKey_ActivityType),Type)
                Dim activity As CompositeActivity = [TryCast](Me.parentDesigner.Activity,CompositeActivity)
                If (((Not type Is Nothing) AndAlso (Not activity Is Nothing)) AndAlso Me.parentDesigner.IsEditable) Then
                    Dim activity2 As Activity = [TryCast](Activator.CreateInstance(type),Activity)
                    Try 
                        CompositeActivityDesigner.InsertActivities(Me.parentDesigner, New HitTestInfo(Me.parentDesigner, HitTestLocations.Designer), New List([Of] Activity)(New Activity() { activity2 }).AsReadOnly, SR.GetString("AddingImplicitActivity"))
                    Catch exception As Exception
                        If (Not exception Is CheckoutException.Canceled) Then
                            Dim service As IUIService = [TryCast](Me.parentDesigner.Activity.Site.GetService(GetType(IUIService)),IUIService)
                            If (Not service Is Nothing) Then
                                service.ShowError(exception.Message)
                            End If
                        End If
                    End Try
                    Dim designer As ActivityDesigner = ActivityDesigner.GetDesigner(activity2)
                    MyBase.UserData.Item(SecondaryView.UserDataKey_Designer) = designer
                End If
            End If
        End Sub


        ' Properties
        Public Overrides ReadOnly Property AssociatedDesigner As ActivityDesigner
            Get
                Dim parentDesigner As ActivityDesigner = [TryCast](MyBase.UserData.Item(SecondaryView.UserDataKey_Designer),ActivityDesigner)
                If (parentDesigner Is Nothing) Then
                    Dim activityType As Type = [TryCast](MyBase.UserData.Item(SecondaryView.UserDataKey_ActivityType),Type)
                    If (activityType Is Nothing) Then
                        Return parentDesigner
                    End If
                    If (Not activityType Is Me.parentDesigner.Activity.GetType) Then
                        Dim activity As Activity = SecondaryViewProvider.FindActivity(Me.parentDesigner, activityType)
                        If (Not activity Is Nothing) Then
                            parentDesigner = ActivityDesigner.GetDesigner(activity)
                        End If
                    Else
                        parentDesigner = Me.parentDesigner
                    End If
                    MyBase.UserData.Item(SecondaryView.UserDataKey_Designer) = parentDesigner
                End If
                Return parentDesigner
            End Get
        End Property


        ' Fields
        Private parentDesigner As StructuredCompositeActivityDesigner
        Friend Shared ReadOnly UserDataKey_ActivityType As Guid = New Guid("03C4103A-D6E9-46e9-B98E-149E145EC2C9")
        Friend Shared ReadOnly UserDataKey_Designer As Guid = New Guid("2B72C7F7-DE4A-4e32-8EB4-9E1ED1C5E84E")
    End Class
End Namespace

